package com.bite.forum.controller;


import com.bite.forum.common.AppConfig;
import com.bite.forum.common.AppResult;
import com.bite.forum.common.ResultCode;
import com.bite.forum.exception.ApplicationException;
import com.bite.forum.model.Message;
import com.bite.forum.model.User;
import com.bite.forum.service.impl.MessageService;
import com.bite.forum.service.impl.UserServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/send")
    public AppResult send(HttpServletRequest request ,
                            @RequestParam("receiveUserId") @NonNull Long receiveUserId ,
                            @RequestParam("content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }

        if (user.getId() == receiveUserId) {
            return AppResult.failed("不能给自己发送站内信");
        }

        User receiveUser = userService.selectByPrimaryKey(receiveUserId);
        if(receiveUser == null || receiveUser.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        Message message = new Message();

        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);

        messageService.create(message);
        return AppResult.success();
    }

    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        HttpSession session = request.getSession(false);

        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        Integer count = messageService.selectUnreadCount(user.getId());

        return AppResult.success(count);
    }


    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user ==  null){
            log.warn(ResultCode.FAILED_NOLOGIN.getMessage());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOLOGIN));
        }

        List<Message> messages = messageService.selectByReceiveUserId(user.getId());

        return AppResult.success(messages);
    }

    @PostMapping("/markRead")
    public AppResult markRead (HttpServletRequest request,
                                @RequestParam("id") @NonNull Long id) {
        // 1. 根据Id查询站内信
        Message message = messageService.selectById(id);
        // 2. 站内信是否存在
        if (message == null || message.getDeleteState() == 1) {
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        if(!user.getId().equals(message.getReceiveUserId())){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        messageService.updateStateById(id , (byte)1);
        return AppResult.success();
    }

    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request ,
                           @RequestParam("repliedId") @NonNull Long repliedId ,
                           @RequestParam("content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.warn(ResultCode.FAILED_UNAUTHORIZED.getMessage());

            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED.getMessage());
        }
        Message repliedMessage = messageService.selectById(repliedId);
        if (repliedMessage == null || repliedMessage.getDeleteState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        if(user.getId() == repliedMessage.getPostUserId()){
            log.warn("不能给自己回复信息");

            return AppResult.failed("不能给自己回复信息");
        }
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(repliedMessage.getPostUserId());
        message.setContent(content);
        messageService.reply(repliedId , message);
        return AppResult.success();
    }
}



































